package LinkedList;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class LeetCode_02_AddTwoNumbers {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {
        }
        ListNode(int val) {
            this.val = val;
        }
        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);
        ListNode cur = head;
        int carry = 0;
        while(l1 != null || l2 !=null || carry != 0){
            int var1 = l1 ==null ?0:l1.val;
            int var2 = l2 == null ?0:l2.val;
            int sum = var1 + var2 + carry;
            carry = sum /10;
            cur.next = new ListNode(sum % 10);
            cur = cur.next;
            if(l1 != null) l1 = l1.next;
            if(l2 != null) l2 = l2.next;
        }
        return head.next;
    }


    public static void main(String[] args) {
        ListNode listNode = new ListNode(2);
        listNode.next = new ListNode(4);
        listNode.next.next = new ListNode(9);

        ListNode listNode2 = new ListNode(5);
        listNode2.next = new ListNode(6);
        listNode2.next.next = new ListNode(4);
        listNode2.next.next.next = new ListNode(9);
        ListNode listNode1 = addTwoNumbers(listNode, listNode2);
    }

}
